home *** CD-ROM | disk | FTP | other *** search
/ BBS in a Box 15 / BBS in a box XV-2.iso / Files II / Prog / S / Simple Sockets 1.0.1.sit / Simple Sockets 1.0.1 ƒ / MacTCP Headers ƒ / Pre - Universal Headers / MacTCP Headers / dnr.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-08-10  |  6.4 KB  |  292 lines  |  [TEXT/MPS ]

  1. /*     DNR.c - DNR library for MPW
  2.  
  3.     (c) Copyright 1988 by Apple Computer.  All rights reserved
  4.     
  5.     Modifications by Jim Matthews, Dartmouth College, 5/91
  6.     
  7. */
  8.  
  9. #include <OSUtils.h>
  10. #include <Errors.h>
  11. #include <Files.h>
  12. #include <Resources.h>
  13. #include <Memory.h>
  14. #include <Traps.h>
  15. #include <GestaltEqu.h>
  16. #include <Folders.h>
  17. #include <ToolUtils.h>
  18.  
  19. #define OPENRESOLVER    1
  20. #define CLOSERESOLVER    2
  21. #define STRTOADDR        3
  22. #define    ADDRTOSTR        4
  23. #define    ENUMCACHE        5
  24. #define ADDRTONAME        6
  25. #define    HINFO            7
  26. #define MXINFO            8
  27.  
  28. Handle codeHndl = nil;
  29.  
  30. typedef OSErr (*OSErrProcPtr)();
  31. OSErrProcPtr dnr = nil;
  32.  
  33.  
  34. TrapType GetTrapType(theTrap)
  35. unsigned long theTrap;
  36. {
  37.     if (BitAnd(theTrap, 0x0800) > 0)
  38.         return(ToolTrap);
  39.     else
  40.         return(OSTrap);
  41.     }
  42.     
  43. Boolean TrapAvailable(trap)
  44. unsigned long trap;
  45. {
  46. TrapType trapType = ToolTrap;
  47. unsigned long numToolBoxTraps;
  48.  
  49.     if (NGetTrapAddress(_InitGraf, ToolTrap) == NGetTrapAddress(0xAA6E, ToolTrap))
  50.         numToolBoxTraps = 0x200;
  51.     else
  52.         numToolBoxTraps = 0x400;
  53.  
  54.     trapType = GetTrapType(trap);
  55.     if (trapType == ToolTrap) {
  56.         trap = BitAnd(trap, 0x07FF);
  57.         if (trap >= numToolBoxTraps)
  58.             trap = _Unimplemented;
  59.         }
  60.     return(NGetTrapAddress(trap, trapType) != NGetTrapAddress(_Unimplemented, ToolTrap));
  61.  
  62. }
  63.  
  64. void GetSystemFolder(short *vRefNumP, long *dirIDP)
  65. {
  66.     SysEnvRec info;
  67.     long wdProcID;
  68.     
  69.     SysEnvirons(1, &info);
  70.     if (GetWDInfo(info.sysVRefNum, vRefNumP, dirIDP, &wdProcID) != noErr) {
  71.         *vRefNumP = 0;
  72.         *dirIDP = 0;
  73.         }
  74.     }
  75.  
  76. void GetCPanelFolder(short *vRefNumP, long *dirIDP)
  77. {
  78.     Boolean hasFolderMgr = false;
  79.     long feature;
  80.     
  81.     if (TrapAvailable(_GestaltDispatch)) if (Gestalt(gestaltFindFolderAttr, &feature) == noErr) hasFolderMgr = true;
  82.     if (!hasFolderMgr) {
  83.         GetSystemFolder(vRefNumP, dirIDP);
  84.         return;
  85.         }
  86.     else {
  87.         if (FindFolder(kOnSystemDisk, kControlPanelFolderType, kDontCreateFolder, vRefNumP, dirIDP) != noErr) {
  88.             *vRefNumP = 0;
  89.             *dirIDP = 0;
  90.             }
  91.         }
  92.     }
  93.     
  94. /* SearchFolderForDNRP is called to search a folder for files that might 
  95.     contain the 'dnrp' resource */
  96. short SearchFolderForDNRP(long targetType, long targetCreator, short vRefNum, long dirID)
  97. {
  98.     HParamBlockRec fi;
  99.     Str255 filename;
  100.     short refnum;
  101.     
  102.     fi.fileParam.ioCompletion = nil;
  103.     fi.fileParam.ioNamePtr = filename;
  104.     fi.fileParam.ioVRefNum = vRefNum;
  105.     fi.fileParam.ioDirID = dirID;
  106.     fi.fileParam.ioFDirIndex = 1;
  107.     
  108.     while (PBHGetFInfo(&fi, false) == noErr) {
  109.         /* scan system folder for driver resource files of specific type & creator */
  110.         if (fi.fileParam.ioFlFndrInfo.fdType == targetType &&
  111.             fi.fileParam.ioFlFndrInfo.fdCreator == targetCreator) {
  112.             /* found the MacTCP driver file? */
  113.             refnum = HOpenResFile(vRefNum, dirID, filename, fsRdPerm);
  114.             if (GetIndResource('dnrp', 1) == NULL)
  115.                 CloseResFile(refnum);
  116.             else
  117.                 return refnum;
  118.             }
  119.         /* check next file in system folder */
  120.         fi.fileParam.ioFDirIndex++;
  121.         fi.fileParam.ioDirID = dirID;    /* PBHGetFInfo() clobbers ioDirID */
  122.         }
  123.     return(-1);
  124.     }    
  125.  
  126. /* OpenOurRF is called to open the MacTCP driver resources */
  127.  
  128. short OpenOurRF()
  129. {
  130.     short refnum;
  131.     short vRefNum;
  132.     long dirID;
  133.     
  134.     /* first search Control Panels for MacTCP 1.1 */
  135.     GetCPanelFolder(&vRefNum, &dirID);
  136.     refnum = SearchFolderForDNRP('cdev', 'ztcp', vRefNum, dirID);
  137.     if (refnum != -1) return(refnum);
  138.         
  139.     /* next search System Folder for MacTCP 1.0.x */
  140.     GetSystemFolder(&vRefNum, &dirID);
  141.     refnum = SearchFolderForDNRP('cdev', 'mtcp', vRefNum, dirID);
  142.     if (refnum != -1) return(refnum);
  143.         
  144.     /* finally, search Control Panels for MacTCP 1.0.x */
  145.     GetCPanelFolder(&vRefNum, &dirID);
  146.     refnum = SearchFolderForDNRP('cdev', 'mtcp', vRefNum, dirID);
  147.     if (refnum != -1) return(refnum);
  148.         
  149.     return -1;
  150.     }    
  151.  
  152.  
  153. OSErr OpenResolver(fileName)
  154. char *fileName;
  155. {
  156.     short refnum;
  157.     OSErr rc;
  158.     
  159.     if (dnr != nil)
  160.         /* resolver already loaded in */
  161.         return(noErr);
  162.         
  163.     /* open the MacTCP driver to get DNR resources. Search for it based on
  164.        creator & type rather than simply file name */    
  165.     refnum = OpenOurRF();
  166.  
  167.     /* ignore failures since the resource may have been installed in the 
  168.        System file if running on a Mac 512Ke */
  169.        
  170.     /* load in the DNR resource package */
  171.     codeHndl = GetIndResource('dnrp', 1);
  172.     if (codeHndl == nil) {
  173.         /* can't open DNR */
  174.         return(ResError());
  175.         }
  176.     
  177.     DetachResource(codeHndl);
  178.     if (refnum != -1) {
  179.         CloseWD(refnum);
  180.         CloseResFile(refnum);
  181.         }
  182.         
  183.     /* lock the DNR resource since it cannot be reloated while opened */
  184.     HLock(codeHndl);
  185.     dnr = (OSErrProcPtr) *codeHndl;
  186.     
  187.     /* call open resolver */
  188.     rc = (*dnr)(OPENRESOLVER, fileName);
  189.     if (rc != noErr) {
  190.         /* problem with open resolver, flush it */
  191.         HUnlock(codeHndl);
  192.         DisposHandle(codeHndl);
  193.         dnr = nil;
  194.         }
  195.     return(rc);
  196.     }
  197.  
  198.  
  199. OSErr CloseResolver()
  200. {
  201.     if (dnr == nil)
  202.         /* resolver not loaded error */
  203.         return(notOpenErr);
  204.         
  205.     /* call close resolver */
  206.     (void) (*dnr)(CLOSERESOLVER);
  207.  
  208.     /* release the DNR resource package */
  209.     HUnlock(codeHndl);
  210.     DisposHandle(codeHndl);
  211.     dnr = nil;
  212.     return(noErr);
  213.     }
  214.  
  215. OSErr StrToAddr(hostName, rtnStruct, resultproc, userDataPtr)
  216. char *hostName;
  217. struct hostInfo *rtnStruct;
  218. long resultproc;
  219. char *userDataPtr;
  220. {
  221.     if (dnr == nil)
  222.         /* resolver not loaded error */
  223.         return(notOpenErr);
  224.         
  225.     return((*dnr)(STRTOADDR, hostName, rtnStruct, resultproc, userDataPtr));
  226.     }
  227.     
  228. OSErr AddrToStr(addr, addrStr)
  229. unsigned long addr;
  230. char *addrStr;                                    
  231. {
  232.     if (dnr == nil)
  233.         /* resolver not loaded error */
  234.         return(notOpenErr);
  235.         
  236.     (*dnr)(ADDRTOSTR, addr, addrStr);
  237.     return(noErr);
  238.     }
  239.     
  240. OSErr EnumCache(resultproc, userDataPtr)
  241. long resultproc;
  242. char *userDataPtr;
  243. {
  244.     if (dnr == nil)
  245.         /* resolver not loaded error */
  246.         return(notOpenErr);
  247.         
  248.     return((*dnr)(ENUMCACHE, resultproc, userDataPtr));
  249.     }
  250.     
  251.     
  252. OSErr AddrToName(addr, rtnStruct, resultproc, userDataPtr)
  253. unsigned long addr;
  254. struct hostInfo *rtnStruct;
  255. long resultproc;
  256. char *userDataPtr;                                    
  257. {
  258.     if (dnr == nil)
  259.         /* resolver not loaded error */
  260.         return(notOpenErr);
  261.         
  262.     return((*dnr)(ADDRTONAME, addr, rtnStruct, resultproc, userDataPtr));
  263.     }
  264.  
  265.  
  266. extern OSErr HInfo(hostName, returnRecPtr, resultProc, userDataPtr)
  267. char *hostName;
  268. struct returnRec *returnRecPtr;
  269. long resultProc;
  270. char *userDataPtr;
  271. {
  272.     if (dnr == nil)
  273.         /* resolver not loaded error */
  274.         return(notOpenErr);
  275.         
  276.     return((*dnr)(HINFO, hostName, returnRecPtr, resultProc, userDataPtr));
  277.  
  278.     }
  279.     
  280. extern OSErr MXInfo(hostName, returnRecPtr, resultProc, userDataPtr)
  281. char *hostName;
  282. struct returnRec *returnRecPtr;
  283. long resultProc;
  284. char *userDataPtr;
  285. {
  286.     if (dnr == nil)
  287.         /* resolver not loaded error */
  288.         return(notOpenErr);
  289.         
  290.     return((*dnr)(MXINFO, hostName, returnRecPtr, resultProc, userDataPtr));
  291.  
  292.     };